package br.ucb.ucblive.HIB;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;

import br.ucb.ucblive.beans.Aluno;
import br.ucb.ucblive.beans.Curso;
import br.ucb.ucblive.beans.Disciplina;
import br.ucb.ucblive.beans.Moderador;
import br.ucb.ucblive.beans.Professor;
import br.ucb.ucblive.beans.Usuario;

public class CursoHIB{

	@SuppressWarnings("unchecked")
	public List<Curso> listar() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Curso.class).list();
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Curso> listarOrdemCrescente() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Curso.class).
				   addOrder(Order.asc("this.nome")).
				   list();
		} finally {
			session.close();
		}
	}
	
	public void excluir(Curso obj) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(obj);
		transaction.commit();
		session.close();
	}
	public void salvar(Curso obj) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(obj);
		transaction.commit();
		session.close();
	}
	
	
	public Curso consultar(int id) {
		Session session = HibernateUtil.getSession();
		try {
			Curso obj = (Curso) session.get(Curso.class,id);
			return obj;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Disciplina> listarDisciplinasByCurso(int idCurso) {
		Session session = HibernateUtil.getSession();
		try {
				List<Disciplina> disciplinas = session.createCriteria(Disciplina.class).
			    createAlias("disciplinaCurso", "curso", JoinType.INNER_JOIN).
			    add(Restrictions.eq("curso.idCurso", idCurso)).
			    list();
			return disciplinas;
		} finally {
			session.close();
		}
	}
	
	public Moderador consultarModeradorByCurso(int idCurso) {
		Session session = HibernateUtil.getSession();
		try {
			Moderador mod = (Moderador) session.createCriteria(Moderador.class).
						add(Restrictions.eq("curso.idCurso", idCurso)).
						setMaxResults(1).
						uniqueResult();
			return mod;
		} finally {
			session.close();
		}
	}
	

	@SuppressWarnings("unchecked")
	public List<Curso> listarCursosByProfessor(Professor professor) {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Curso.class).
				   createAlias("disciplinaCurso", "disciplina").
				   createAlias("disciplina.disciplinaProfessor", "professor").
				   createAlias("professor.usuario", "usuario").
				   setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).
				   add(Restrictions.eq("usuario.login", professor.getUsuario().getLogin())).
				   list();
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Aluno> listarAlunosByCurso(Curso curso){
		Session session = HibernateUtil.getSession();
		try{
			return session.createCriteria(Aluno.class).
					createAlias("curso","curso").
					add(Restrictions.eq("curso.idCurso",curso.getIdCurso())).
					list();
		}finally {
			session.close();
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Curso> listarCursosByUsuario(Usuario usuario) {
		Session session = HibernateUtil.getSession();
		try {
			if(usuario.getTipo()==1){
				return session.createCriteria(Curso.class).
					   createAlias("moderador", "moderador").
					   createAlias("moderador.usuario", "usuario").
					   add(Restrictions.eq("usuario.login", usuario.getLogin())).
					   list();
			}
			else if(usuario.getTipo()==2){
				return session.createCriteria(Curso.class).
					   createAlias("disciplinaCurso", "disciplina").
					   createAlias("disciplina.disciplinaProfessor", "professor").
					   createAlias("professor.usuario", "usuario").
					   setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).
					   add(Restrictions.eq("usuario.login", usuario.getLogin())).
					   list();
			}
			else if(usuario.getTipo()==3){
				return session.createCriteria(Curso.class).
					   createAlias("alunos", "aluno").
					   createAlias("aluno.usuario", "usuario").
					   add(Restrictions.eq("usuario.login", usuario.getLogin())).
					   list();
			}
		} finally {
			session.close();
		}
		return new ArrayList<Curso>();
	}

	@SuppressWarnings("unchecked")
	public List<Curso> listarCursosModeradorIsCadastro() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Curso.class).
				   createAlias("moderador", "moderador", JoinType.LEFT_OUTER_JOIN).
				   add(Restrictions.isNull("moderador.curso")).
				   list();
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Curso> listarCursosModeradorIsAtualiza(int idCurso) {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Curso.class).
				   createAlias("moderador", "moderador", JoinType.LEFT_OUTER_JOIN).
				   add((Restrictions.or(Restrictions.eq("moderador.curso.idCurso", idCurso), Restrictions.isNull("moderador.curso")))).
				   list();
		} finally {
			session.close();
		}
	}
	
	
}
